<!DOCTYPE html>

<!--
TODO(majidvp): The try/catch in above test cases should not be needed but at the moment
threaded worklet does not correctly pass error to its parent context. It crashes in
https://codesearch.chromium.org/chromium/src/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.cc?sq=package:chromium&dr&l=320
-->
<script id="duplicate" type="text/worklet">
const expectedError = "A class with name:'duplicate' is already registered";
let receivedError = undefined;
try {
  registerAnimator("duplicate", class { animate() {} });
  registerAnimator("duplicate", class { animate() {} });
} catch(e) {
  receivedError = e.toString();
}
if (!receivedError || receivedError.indexOf(expectedError) < 0)
  console.log(`FAIL: expected=${expectedError}, received=${receivedError}`);
</script>

<script id="no_class" type="text/worklet">
const expectedError = "TypeError: Failed to execute 'registerAnimator' on 'AnimationWorkletGlobalScope': parameter 2 is not of type 'Function'."
let receivedError = undefined;
try {
  registerAnimator("no_class", "");
} catch(e) {
  receivedError = e.toString();
}
if (!receivedError || receivedError.indexOf(expectedError) < 0)
  console.log(`FAIL: expected=${expectedError}, received=${receivedError}`);
</script>

<script id="empty_string" type="text/worklet">
const expectedError = "The empty string is not a valid name";
let receivedError = undefined;
try {
  registerAnimator("", class { animate() {} });
} catch(e) {
  receivedError = e.toString();
}
if (!receivedError || receivedError.indexOf(expectedError) < 0)
  console.log(`FAIL: expected=${expectedError}, received=${receivedError}`);
</script>

<script id="legit" type="text/worklet">
let receivedError = undefined;
try {
  registerAnimator("test", class {
    constructor() {}
    animate() {}
  });
} catch(e) {
  // Exception should not be thrown.
  receivedError = "An error occurred";
}
if (receivedError)
  console.log(`FAIL: unexpected error: ${receivedError}`);
</script>

<script src="resources/animation-worklet-tests.js"></script>
<script>
  runAnimationWorkletTests();
</script>
